<div id="sf_admin_container">
        <script>
            function doScroll( element, event )
            {
                var parent = element;
                while( parent.nodeName != "TABLE" )
                {
                    parent = parent.parentNode;
                }            
               
                var divs = parent.getElementsByTagName( 'DIV' );
               
                for ( var i = 0; i < divs.length; i++ )
                {
                    if( divs[i].id == "colHeaders" )
                    {
                        divs[i].scrollLeft = element.scrollLeft;
                    }

                    if( divs[i].id == "rowHeaders" )
                    {
                        divs[i].scrollTop = element.scrollTop;
                    }                   
                }
            }
        </script>

<?php 
$date_inf = getdate();
$events = array();
foreach ( $apartments as $apartment ):
  $events[$apartment->getId()]['days'] = '0';  
  $bookings = $apartment->getApartmanBookings();
  foreach ($bookings as $b) {
    $days = (strtotime($b->getCheckOut()) - strtotime($b->getCheckIn())) / (60 * 60 * 24);
    $d = str_replace("-","",$b->getCheckIn());
    $da = explode(" ",$d );
    $events[$b->getApartmanId()][$da[0]][0] = ceil($days);
    $events[$b->getApartmanId()][$da[0]][1] = $b->getId();
  }
endforeach;


foreach( $events as $apartman_id => $bookings )
{
  $sum = 0;  
  foreach( $bookings as $key => $value )
  {
	$days = $value[0];
	$sum += $days;
  }
  $events[$apartman_id]['days'] = $sum;
}

$colors = array( 'color1', 'color2', 'color3', 'color4' );
?>
<div>
<table class="table" cellspacing="0" cellpadding="0">
  <tr>
      <td>
      </td>
      <td>
          <div ID="colHeaders" >
              <table cellspacing="0" cellpadding="0">
                  <tr>
                  <?php 
                    for ( $i = 1; $i <= 12; ++$i ){
                      $md = getMonthDays($date_inf['year'],$i);
                      for( $j = 1; $j <= $md; ++$j){
					  $day = date( "w", mktime( 0, 0, 0, $i, $j, $date_inf['year'] ) );
					  switch( $day )
					  {
						case 0:
						  $class = 'day-sun';
						break;
						case 6:
						  $class = 'day-sat';
						break;
						default:
						  $class= '';
						break;
					  }
                        echo "<th class='".$class."'><p>{$i}/{$j}</p></th>";
                      }
                    }
                    ?>
                  </tr>
              </table>
          </div>                      
      </td>
  </tr>
  <tr>
  	<td>
  		<div ID="rowHeaders">
        	<table cellspacing="0" cellpadding="0">
        		<?php 
        		  foreach ( $apartments as $apartment ){
  			        $bookings = $apartment->getApartmanBookings();
        		    echo "<tr><th><p>" . link_to($apartment->getName(), 'apartman/timeTable?id='.$apartment->getId() );
  			        echo "(".$events[$apartment->getId()]['days'].")</p></th></tr>";
        		  }
        		?>                           
        	</table>
    	</div>
  	</td>
  	<td>
  		<div ID="body" onScroll="doScroll( this, event )">
            <table cellspacing="0" cellpadding="0">
                <?php foreach ( $apartments as $apartment ):?>
        		<tr>
                    <?php
              		$tmp = 0;
              		$class = 1;
              		$b_id = '';
              		for ($i = 1; $i <= 12; ++$i){
              			$md = getMonthDays($date_inf['year'],$i);
              			for($j=1; $j <= $md; ++$j){
							$day = date( "w", mktime( 0, 0, 0, $i, $j, $date_inf['year'] ) );
							switch( $day )
							{
							  case 0:
								$day_class = 'day-sun';
							  break;
							  case 6:
								$day_class = 'day-sat';
							  break;
							  default:
								$day_class= '';
							  break;
							}							
              				$d = $date_inf['year'].sprintf("%02d", $i).sprintf("%02d", $j);
              				if( isset($events[$apartment->getId()][$d]) and  $events[$apartment->getId()][$d][0] > 0 ){
              					$tmp = $events[$apartment->getId()][$d][0];
              					$b_id = $events[$apartment->getId()][$d][1];
              					$class *= -1;
              				}
              				if( $tmp > 0 ){
              					$bg = 'blue';
              					if( $class > 0 ){
              						$bg = 'red';
              					}
								
								$last = $tmp == 1 ? 'last' : '';
              					echo "<td class='".$day_class."' id='{$b_id}'><p class='".$bg." ".$last."'>&nbsp;</p></td>";
              					$tmp--;
              				}else{
              					echo "<td class='".$day_class."'><p>&nbsp;</p></td>";
              				}
              			}
              		}
              	    ?>
                </tr>
                <?php endforeach; ?>
        	</table>
        </div>
  	</td>
  </tr>
</table>
</div>
</div>

<?php
function getMonthDays($year, $month){
	switch ($month) {
            case 02:
                $a = $year / 4;
                $b = ceil($a);
                if ($a==$b){               
                     $maxdays = 29;
                } else {
                      $maxdays = 28;
                }
                break;
            case 04:
                $maxdays = 30;
                break;
            case 06:
                $maxdays = 30;
                break;
            case 09:
                $maxdays = 30;
                break;
            case 11:
                $maxdays = 30;
                break;
            default:
            	$maxdays = 31;
            	break;
        }
	return $maxdays;
}


?>